package brainsplash.gyrusscope.game;

import java.lang.Math; 
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import brainsplash.gyrusscope.framework.gl.TextureRegion;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;
import android.util.Log;

public class BaseObject {
	public TextureRegion sprite;
	public float speed;
	public float direction; //degrees
	public int x; //consider float
	public int y; //consider float
	public int center_x;
	public int center_y;
	public float distanceFromCenter;
	public float minDistFromCenter = 10;
	public int radius; //collisions
	public float size;

	
	private FloatBuffer vertexBuffer;	// buffer holding the vertices
	private float vertices[] = {
			-1.0f, -1.0f,  0.0f,		// V1 - bottom left
			-1.0f,  1.0f,  0.0f,		// V2 - top left
			 1.0f, -1.0f,  0.0f,		// V3 - bottom right
			 1.0f,  1.0f,  0.0f			// V4 - top right
	};

	private FloatBuffer textureBuffer;	// buffer holding the texture coordinates
	private float texture[] = {
			// Mapping coordinates for the vertices
			0.0f, 1.0f,		// top left		(V2)
			0.0f, 0.0f,		// bottom left	(V1)
			1.0f, 1.0f,		// top right	(V4)
			1.0f, 0.0f		// bottom right	(V3)
	};
	
	public BaseObject(TextureRegion sprite, int x, int y, int radius) {
		this(sprite, x, y, (int) WorldRenderer.FRUSTUM_WIDTH / 2, (int) WorldRenderer.FRUSTUM_HEIGHT / 2, radius, 0, 0, 0);
	}
	
	public BaseObject(TextureRegion sprite, int x, int y, int radius, float distanceFromCenter, float direction, float speed) {
		this(sprite, x, y, (int) WorldRenderer.FRUSTUM_WIDTH / 2, (int) WorldRenderer.FRUSTUM_HEIGHT / 2, radius, distanceFromCenter, direction, speed);
	}
	
	public BaseObject(TextureRegion sprite, int x, int y, int center_x, int center_y, 
			int radius, float distanceFromCenter, float direction, float speed) {
		this.sprite = sprite;
		this.x = x;
		this.y = y;
		this.center_x = center_x;
		this.center_y = center_y;
		this.radius = radius;
		this.distanceFromCenter = distanceFromCenter;
		this.direction = direction;
		this.speed = speed;

		// a float has 4 bytes so we allocate for each coordinate 4 bytes
		ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4);
		byteBuffer.order(ByteOrder.nativeOrder());
		
		// allocates the memory from the byte buffer
		vertexBuffer = byteBuffer.asFloatBuffer();
		
		// fill the vertexBuffer with the vertices
		vertexBuffer.put(vertices);
		
		// set the cursor position to the beginning of the buffer
		vertexBuffer.position(0);
		
		byteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
		byteBuffer.order(ByteOrder.nativeOrder());
		textureBuffer = byteBuffer.asFloatBuffer();
		textureBuffer.put(texture);
		textureBuffer.position(0);
	}
	
	public void update() {
		//quick solution
		distanceFromCenter = Math.max(distanceFromCenter - speed * (distanceFromCenter / 100), minDistFromCenter);
		
		//update coordinates
		x = (int) (center_x + (Math.cos(Math.toRadians(direction)) * distanceFromCenter));
		y = (int) (center_y + (Math.sin(Math.toRadians(direction)) * distanceFromCenter));
		
		float distCenterScreen = (float) Math.sqrt(Math.pow(x - WorldRenderer.FRUSTUM_WIDTH / 2, 2) + Math.pow(y - WorldRenderer.FRUSTUM_HEIGHT / 2, 2));
		
		size = Math.max(Math.min((radius*2) * (distCenterScreen / 100), radius*2), radius*0.5f);
	}
	


	public void draw(GL10 gl) {
        //Log.d(">>>>>>>", "draw Ran");
		// bind the previously generated texture
		//gl.glBindTexture(GL10.GL_TEXTURE_2D, ((GlRenderer) (gl)).textures[sprite]);
		
		// Point to our buffers
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		
		// Set the face rotation
		gl.glFrontFace(GL10.GL_CW);
		
		// Point to our vertex buffer
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
		
		// Draw the vertices as triangle strip
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);

		//Disable the client state before leaving
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	}
	
	
	
}
